module Versioning
    ###############################################################################
    # Search in a AssemblyInfo.cs file for the 3 first parts of AssemblyVersion and 
    # concat to it a build number
    # So if assemblyinfo_filename contains "[assembly: AssemblyVersion("1.2.3.4")]" and build_number equals 10
    # the method returns "1.2.3.10"
    def self.retrieve_dotnet_version(assemblyinfo_filename, build_number)
        version = '0.0.0'
        text = File.read(assemblyinfo_filename)
        version = $1 if text =~ /AssemblyVersion\("(\d+\.\d+\.\d+)/
        version = version + '.' + build_number    
    end

    ###############################################################################
    # Update C++ version info into resource file with a build number.
    def self.update_cpp_version(resource_file_name, build_number)
        puts "updating #{resource_file_name} with #{build_number}"
        text = File.read(resource_file_name)
        # update StringFileInfo block
        text.gsub!(/(\"(File|Product)Version\",\s+\"(\d+(,|\.)\s*){3})(\d+)\"/, '\1' + build_number + '"')
        # update VERSIONINFO block
        text.gsub!(/((FILE|PRODUCT)VERSION\s+(\d+,){3})(\d+)/, '\1' + build_number)
        file = File.new(resource_file_name, 'w')
        file.puts(text)
        file.close
    end
end

